package com.lma.crm.post.domain.web.action;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.lma.crm.post.domain.CrmPost;
import com.lma.crm.post.service.CrmPostService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

public class CrmPostAction extends ActionSupport implements ModelDriven<CrmPost>{

    private CrmPost crmPost = new CrmPost();
    
    @Override
    public CrmPost getModel() {
        return crmPost;
    }
    
    private CrmPostService crmPostService;
    public void setCrmPostService(CrmPostService crmPostService) {
        this.crmPostService = crmPostService;
    }    
    ///////////////////////////////////////////////////////////

    public String findPostWithDepartment() throws Exception {
        
        System.out.println(crmPostService);
        
        //1.查询
        List<CrmPost> allPost = crmPostService.findAll(crmPost.getDepartment());
        
        //2.将java对象转换为json对象
        //2.1 排除不需要的数据
        JsonConfig config = new JsonConfig();
        config.setExcludes(new String[]{"department", "staffSet"});
        //2.2 转换
        String jsonData = JSONArray.fromObject(allPost,config).toString();
        
        
        //3.将json数据发送到前台
        //3.1 解决前台乱码问题
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        //3.2 发送数据
        ServletActionContext.getResponse().getWriter().write(jsonData);
        
        return "none";
    }
    
}
